*********************************************************************************
*							Estimate Bunching Elasticity - admin ratio	*
*							Jennifer Mayo									*
*							Date: 21/02/2020									*
*********************************************************************************


clear all
set more off

global name "jmp4k"
global home "/Users/$name/Library/CloudStorage/OneDrive-UniversityofMissouri/Projects/Navigating the notches/"
global data "$home/Replication"
global output "$home/CN Output"


use "$data/FinancialMetrics.dta", clear


*********************************************************************************
*	ADMIN RATIO																	*
*********************************************************************************
	
*********************************************************************************
*	Collapse Data into counts of firms by ratio bins							*
*********************************************************************************

	keep if justone == 1 & administrationexpensesratio>=0
	
	drop if PublicationYear==2011  
	drop if PublicationYear==2016  // drop years method changed	
	duplicates drop
	
	gen adminratio = administrationexpensesratio*100
	
	egen double adminscoregroups = cut(adminratio), at(0 (0.2) 100)

	gen wt = round(adminratio,1) // create an integer weight to use as frequency weights

			
		
		
*********************************************************************************
*	Define Programs to Estimate Bunching and Bootstrap SE				*
*********************************************************************************
	
	* Define Bunching Estimate Program: 

	do "$data/adminbunchest.do" // Create bunching estimator program

	* Define Bootstrap Estimate Program: 
	
	cap program drop bunchestse
	program define bunchestse, rclass
		args lb threshold
		
		preserve
		
		bsample, cluster(ein)
		adminbunchest `lb' `threshold'
		

		return scalar ub_est = r(ub_est)
		return scalar excess_est = r(excess_est)
		return scalar b_av_est = r(b_av_est)				
		
	restore
	end
	

*********************************************************************************
*	Estimate Bunching on "other" charities, 0.15 admin threshold, all years/annual 		*
********************************************************************************* 

	* Restrict sample: 

	preserve
	drop if adminratio>18
	keep if causeid ==1 | causeid==2 | causeid>3 & causeid<18 | causeid==19 | causeid==20 | causeid==21 | causeid>22 & causeid<30| causeid>30
	
	count
	local n_1 = r(N)
	file open myfile using "$output/admin_bunching_estimates_13obstotal.tex", write text replace 
	file write myfile "`n_1'\unskip" 
	file close myfile
	
	adminbunchest 13 15  // args: lb and threshold
	mat res1 = (r(ub_est),r(excess_est),r(b_av_est))
	
	matlist res1
	
	restore 

	*Bootstrap SE: 
	
	preserve
	drop if adminratio>18
	keep if causeid ==1 | causeid==2 | causeid>3 & causeid<18 | causeid==19 | causeid==20 | causeid==21 | causeid>22 & causeid<30| causeid>30
	
	simulate ub=r(ub_est) excess = r(excess_est) b_av = r(b_av_est), reps(100) seed(1234): bunchestse 13 15
	
	bstat, stat(res1) n(`n_1')
	
	mat A_1 = r(table)
	
	restore

* 2019
	
	preserve
	drop if adminratio>18
	keep if causeid ==1 | causeid==2 | causeid>3 & causeid<18 | causeid==19 | causeid==20 | causeid==21 | causeid>22 & causeid<30| causeid>30
	keep if PublicationYear == 2019	
	
	count
	local n_2 = r(N)
	file open myfile using "$output/admin_bunching_estimates_13obs2019.tex", write text replace 
	file write myfile "`n_2'\unskip" 
	file close myfile
	
	adminbunchest 13 15  // args: lb and threshold
	mat res2 = (r(ub_est),r(excess_est),r(b_av_est))
	
	matlist res2
	
	restore 

	*Bootstrap SE: 
	
	preserve
	drop if adminratio>18
	keep if causeid ==1 | causeid==2 | causeid>3 & causeid<18 | causeid==19 | causeid==20 | causeid==21 | causeid>22 & causeid<30| causeid>30
	keep if PublicationYear == 2019		
	
	simulate ub=r(ub_est) excess = r(excess_est) b_av = r(b_av_est), reps(100) seed(1234): bunchestse 13 15
	
	bstat, stat(res2) n(`n_2')
	
	mat A_2 = r(table)
	
	restore

* 2018
	
	preserve
	drop if adminratio>18
	keep if causeid ==1 | causeid==2 | causeid>3 & causeid<18 | causeid==19 | causeid==20 | causeid==21 | causeid>22 & causeid<30| causeid>30
	keep if PublicationYear == 2018	
	
	count
	local n_3 = r(N)
	file open myfile using "$output/admin_bunching_estimates_13obs2018.tex", write text replace 
	file write myfile "`n_3'\unskip" 
	file close myfile
	
	adminbunchest 13 15  // args: lb and threshold
	mat res3 = (r(ub_est),r(excess_est),r(b_av_est))
	
	matlist res3
	
	restore 

	*Bootstrap SE: 
	
	preserve
	drop if adminratio>18
	keep if causeid ==1 | causeid==2 | causeid>3 & causeid<18 | causeid==19 | causeid==20 | causeid==21 | causeid>22 & causeid<30| causeid>30
	keep if PublicationYear == 2018		
	
	simulate ub=r(ub_est) excess = r(excess_est) b_av = r(b_av_est), reps(100) seed(1234): bunchestse 13 15
	
	bstat, stat(res3) n(`n_3')
	
	mat A_3 = r(table)
	
	restore
	
* 2017
	
	preserve
	drop if adminratio>18
	keep if causeid ==1 | causeid==2 | causeid>3 & causeid<18 | causeid==19 | causeid==20 | causeid==21 | causeid>22 & causeid<30| causeid>30
	keep if PublicationYear == 2017	
	
	count
	local n_4 = r(N)
	file open myfile using "$output/admin_bunching_estimates_13obs2017.tex", write text replace 
	file write myfile "`n_4'\unskip" 
	file close myfile
	
	adminbunchest 13 15  // args: lb and threshold
	mat res4 = (r(ub_est),r(excess_est),r(b_av_est))
	
	matlist res4
	
	restore 

	*Bootstrap SE: 
	
	preserve
	drop if adminratio>18
	keep if causeid ==1 | causeid==2 | causeid>3 & causeid<18 | causeid==19 | causeid==20 | causeid==21 | causeid>22 & causeid<30| causeid>30
	keep if PublicationYear == 2017		
	
	simulate ub=r(ub_est) excess = r(excess_est) b_av = r(b_av_est), reps(100) seed(1234): bunchestse 13 15
	
	bstat, stat(res4) n(`n_4')
	
	mat A_4 = r(table)
	
	restore

* 2015
	
	preserve
	drop if adminratio>18
	keep if causeid ==1 | causeid==2 | causeid>3 & causeid<18 | causeid==19 | causeid==20 | causeid==21 | causeid>22 & causeid<30| causeid>30
	keep if PublicationYear == 2015	
	
	count
	local n_5 = r(N)
	file open myfile using "$output/admin_bunching_estimates_13obs2015.tex", write text replace 
	file write myfile "`n_5'\unskip" 
	file close myfile
	
	adminbunchest 13 15  // args: lb and threshold
	mat res5 = (r(ub_est),r(excess_est),r(b_av_est))
	
	matlist res5
	
	restore 

	*Bootstrap SE: 
	
	preserve
	drop if adminratio>18
	keep if causeid ==1 | causeid==2 | causeid>3 & causeid<18 | causeid==19 | causeid==20 | causeid==21 | causeid>22 & causeid<30| causeid>30
	keep if PublicationYear == 2015		
	
	simulate ub=r(ub_est) excess = r(excess_est) b_av = r(b_av_est), reps(100) seed(1234): bunchestse 13 15
	
	bstat, stat(res5) n(`n_5')
	
	mat A_5 = r(table)
	
	restore	


		
*********************************************************************************
* Step 7: Create Table for Paper												*
*********************************************************************************

	*All years pt. estimates: 
	
	mat r1 =  [round(A_1[1,3],.001)]
	
	* 2019 pt. estimates: 
	
	mat r2 =  [round(A_2[1,3],.001)]
	
	* 2018 pt. estimates: 
	
	mat r3 =  [round(A_3[1,3],.001)]
	
	* 2017 pt. estimates: 
	
	mat r4 =  [round(A_4[1,3],.001)]
	
	* 2015 pt. estimates: 
	
	mat r5 =  [round(A_5[1,3],.001)]
	
	* All years standard errors: 
	
	mat r11 =  [round(A_1[2,3],.001)]

	* 2019 standard errors: 
	
	mat r12 =  [round(A_2[2,3],.001)]
	
	* 2018 standard errors: 
	
	mat r13 =  [round(A_3[2,3],.001)]
	
	* 2017 standard errors: 
	
	mat r14 =  [round(A_4[2,3],.001)]
	
	* 2015 standard errors: 
	
	mat r15 =  [round(A_5[2,3],.001)]


	estadd matrix r1, replace
	estadd matrix r2, replace
	estadd matrix r3, replace
	estadd matrix r4, replace
	estadd matrix r5, replace
	estadd matrix r11, replace
	estadd matrix r12, replace
	estadd matrix r13, replace
	estadd matrix r14, replace
	estadd matrix r15, replace
	
	esttab using "$output/admin_bunching_estimates_13.tex", ///
		cells("r1 r2 r3 r4 r5" "r11(par) r12(par) r13(par) r14(par) r15(par)") collabels("All Years" "2019" "2018" "2017" "2015") ///
 		coeflabel(c1 "Average bunching response" )   nomtitles ///
		nonumbers gaps booktab frag plain replace

	
	
